<?php

/**
 * Created by PhpStorm.
 * User: Sing
 * Date: 16/11/4
 * Time: 11:44
 */
class dbt_member extends sing_table{
    private $_group;
    private $_level;
    private $_gl;
    private $_mid;
    private $_memerId;
    private $_openid;
    private $_glOrderBy;

    public function __construct(){
        
        $this->_level = array(
            '25'=>4,//水晶
            '28' => 5, // 珍珠会员终身对应的 会员组id
            '27' => 5, // 珍珠会员对应的 会员组id
            '19' => 6, // 黄金会员2对应的 会员组id
            '18' => 6, // 黄金会员1对应的 会员组id
            '23' => 7, // 钻石会员2对应的 会员组id
            '22' => 7, // 钻石会员1对应的 会员组id
            '21' => 8, // 翡翠会员2对应的 会员组id
            '20' => 8 // 翡翠会员1对应的 会员组id
        );
        $this->_group = array(
            '25' => 394, // 水晶会员对应的 会员组id
            '28' => 393, // 珍珠会员终身对应的 会员组id
            '27' => 393, // 珍珠会员对应的 会员组id
            '19' => 397, // 黄金会员2对应的 会员组id
            '18' => 397, // 黄金会员1对应的 会员组id
            '23' => 395, // 钻石会员2对应的 会员组id
            '22' => 395, // 钻石会员1对应的 会员组id
            '21' => 396, // 翡翠会员2对应的 会员组id
            '20' => 396 // 翡翠会员1对应的 会员组id
        );
        $this->_gl = array(
            '0' => 398, //普通
            '4' => 394,//水晶
            '5' => 393, //珍珠
            '6' => 397,//黄金
            '7' => 395,//钻石
            '8' => 396//翡翠
        );
        //会员上下级关系
        $this->_glOrderBy = array(
            398 => 1, //普通
            394 => 2,//水晶
            393 => 3, //珍珠
            397 => 4,//黄金
            396 => 5,//钻石
            395 => 6//翡翠
        );
    }

    public  function  setMemberGroupWithCard($levelid){
        
        $this->_mid = $_GET['mid'];
        $this->_openid = $_GET['openid'];
        $this->_memerId = $_GET['memberId'];
        
        if ($levelid >= 0) {
            $groupId = $this->_gl[$levelid];
        }
        echo "groupId:".$groupId."<br>";
        echo "levelid:".$levelid."<br>";
        if ($groupId) {
            
            $nowUser = C::t("#shake#shake")->shakeUser();
            if(!$nowUser){
                return false;
            }
            
            if($this->_glOrderBy[$groupId]<=$this->_glOrderBy[$nowUser['groupid']]){
                return false;
            }
            
            var_dump($nowUser);
            
            $nowUser['groupid'] = $groupId;
            
            var_dump($nowUser);
            
            C::t("#shake#shake")->shakeUser($nowUser);
            
            $memberId = $this->_memerId;
            
            echo "memberId:".$memberId."<br>";
            
            DB::SetMainDB(DB_MAIN);
            DB::update('common_member_perfect', array("updatetime"=>time(),"levelid"=>$levelid), DB::format('mid=%s and member_id=%d and display=1', array($this->_mid, $memberId)));

            $update = array(
                'gid' => $groupId,
                'updatetime' => TIME
            );
            DB::SetMainDB(DB_MAIN);
            DB::update('common_member', $update, DB::format('mid=%s and id=%d and display=1', array($this->_mid, $memberId)));

            DB::SetMainDB(DB_MAIN);
            $openid = DB::result_first('select openid from %t where id=%d and display=1', array('common_member', $memberId));
            if ($openid) {
                $update = array(
                    'groupid' => $groupId,
                );
                DB::SetMainDB(DB_MAIN);
                $r = DB::result_first('select openid from %t where openid=%s limit 1', array('wx_wxuser', $openid));
                if (!$r) {
                    DB::SetMainDB(DB_MAIN);
                    DB::insert('wx_wxuser', array('openid' => $openid, 'mid' => $this->_mid));
                }
                DB::SetMainDB(DB_MAIN);
                DB::update('wx_wxuser', $update, DB::format('openid=%s', array($openid)));
            }
            
            
        }
    }
}